nativedialog: Do not ref the transient_for parent
authorMatthias Clasen <mclasen@redhat.com>
Sat, 9 May 2020 20:05:08 +0000 (16:05 -0400)
committerMatthias Clasen <mclasen@redhat.com>
Mon, 11 May 2020 16:19:39 +0000 (12:19 -0400)
gtk_window_set_transient_for does not ref its parent either. This is
important because a child widget of the parent might be the one calling
this function.

This was showing up as widget-factory not existing on close after
opening the file chooser.

gtk/gtknativedialog.c

index c97c6c06979feb663302801a70e3d4d3a9da2180..1a060be27adf871ec577416597c31deb91433108 100644 (file)
@@ -168,12 +168,21 @@ gtk_native_dialog_get_property (GObject    *object,
     }
 }
 
+static void parent_destroyed (GtkWidget       *parent,
+                              GtkNativeDialog *self);
+
 static void
 gtk_native_dialog_dispose (GObject *object)
 {
   GtkNativeDialog *self = GTK_NATIVE_DIALOG (object);
   GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
 
+  if (priv->transient_for)
+    {
+      g_signal_handlers_disconnect_by_func (priv->transient_for, parent_destroyed, self);
+      priv->transient_for = NULL;
+    }
+
   if (priv->visible)
     gtk_native_dialog_hide (self);
 
@@ -486,6 +495,15 @@ gtk_native_dialog_get_title (GtkNativeDialog *self)
   return priv->title;
 }
 
+static void
+parent_destroyed (GtkWidget       *parent,
+                  GtkNativeDialog *self)
+{
+  GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
+
+  priv->transient_for = NULL;
+}
+
 /**
  * gtk_native_dialog_set_transient_for:
  * @self: a #GtkNativeDialog
@@ -501,14 +519,24 @@ gtk_native_dialog_get_title (GtkNativeDialog *self)
  */
 void
 gtk_native_dialog_set_transient_for (GtkNativeDialog *self,
-                                     GtkWindow *parent)
+                                     GtkWindow       *parent)
 {
   GtkNativeDialogPrivate *priv = gtk_native_dialog_get_instance_private (self);
 
   g_return_if_fail (GTK_IS_NATIVE_DIALOG (self));
 
-  if (g_set_object (&priv->transient_for, parent))
-    g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_TRANSIENT_FOR]);
+  if (parent == priv->transient_for)
+    return;
+
+  if (priv->transient_for)
+    g_signal_handlers_disconnect_by_func (priv->transient_for, parent_destroyed, self);
+
+  priv->transient_for = parent;
+
+  if (parent)
+    g_signal_connect (parent, "destroy", G_CALLBACK (parent_destroyed), self);
+
+  g_object_notify_by_pspec (G_OBJECT (self), native_props[PROP_TRANSIENT_FOR]);
 }
 
 /**